package svgedit.gui;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import svgedit.gui.actions.ChooseColorAction;
import svgedit.gui.actions.InsertCircleAction;
import svgedit.gui.actions.InsertLineAction;
import svgedit.gui.actions.InsertRectAction;
import svgedit.gui.actions.OpenDocumentAction;
import svgedit.gui.actions.SaveAction;
import svgedit.gui.actions.SizeListener;
import svgedit.svg.SVGPaint;

/**
 * The toolbar that is at the top of the frame, and contains the buttons for
 * opening/saving, changing fill/stroke colour, and stroke width It also has
 * buttons to select a new shape to draw
 * 
 * @author SARJ
 * 
 */
public class Toolbar extends JPanel
{

	private static final long serialVersionUID = 793373007859880420L;
	/** The panels in the toolbar denoting sections */
	private JPanel jpFile;
	private JPanel jpColorFill;
	private JPanel jpQuickShapes;

	/** The little box showing the color in the buttons */
	private JButton btnStrokeColor;
	private JButton btnFillColor;
	boolean fillColorNone;
	boolean strokeColorNone;

	private JTextField txtStrokeWidth;

	/**
	 * Construct the toolbar
	 */
	public Toolbar()
	{
		super();

		// File buttons panel
		jpFile = new JPanel();
		add(jpFile);

		JButton btnOpen = new JButton("Open");
		btnOpen.addActionListener(new OpenDocumentAction());
		jpFile.add(btnOpen);

		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new SaveAction());
		jpFile.add(btnSave);

		// Seperate panels
		add(Box.createHorizontalStrut(25));

		// Quick Shapes Panel
		jpQuickShapes = new JPanel();
		add(jpQuickShapes);

		JButton btnRect = new JButton("Rect");
		btnRect.addActionListener(new InsertRectAction());
		jpQuickShapes.add(btnRect);

		JButton btnCircle = new JButton("Circle");
		btnCircle.addActionListener(new InsertCircleAction());
		jpQuickShapes.add(btnCircle);

		JButton btnLine = new JButton("Line");
		btnLine.addActionListener(new InsertLineAction());
		jpQuickShapes.add(btnLine);

		// Seperate panels
		add(Box.createHorizontalStrut(25));

		// Color Fill Panel
		jpColorFill = new JPanel();
		add(jpColorFill);

		jpColorFill.add(new JLabel("Fill:"));

		// Fill color button
		btnFillColor = new JButton();
		btnFillColor.setBackground(Color.RED);
		btnFillColor.setPreferredSize(new Dimension(25, 20));
		btnFillColor.addActionListener(new ChooseColorAction(btnFillColor,
				ColorChooser.FILL_OPTION));
		jpColorFill.add(btnFillColor);

		jpColorFill.add(new JLabel("Stroke:"));

		// stroke color button
		btnStrokeColor = new JButton();
		btnStrokeColor.setBackground(Color.BLACK);
		btnStrokeColor.setPreferredSize(new Dimension(25, 20));
		btnStrokeColor.addActionListener(new ChooseColorAction(btnStrokeColor,
				ColorChooser.STROKE_OPTION));
		jpColorFill.add(btnStrokeColor);

		// Stroke-width
		// JLabel lblStrokeWidth = new JLabel();
		// jpColorFill.add(lblStrokeWidth);
		txtStrokeWidth = new JTextField("2px", 4);
		txtStrokeWidth.addKeyListener(new SizeListener(txtStrokeWidth));
		jpColorFill.add(txtStrokeWidth);

	}

	/**
	 * Returns the stroke color currently selected
	 * 
	 * @return the stroke Color
	 */
	public SVGPaint getStrokeColor()
	{

		SVGPaint color = new SVGPaint();

		// If stroke colour is none
		if (strokeColorNone == true)
		{
			color.setValueFromString("none");
		}
		else
		{
			color.setRGBColor(btnStrokeColor.getBackground());
		}

		return color;

	}

	/**
	 * Return the stroke width
	 * 
	 * @return the stroke Width
	 */
	public String getStrokeWidth()
	{
		return txtStrokeWidth.getText();

	}

	/**
	 * Returns the fill color currently selected
	 * 
	 * @return The fill color in SVGPaint
	 */
	public SVGPaint getFillColor()
	{

		SVGPaint color = new SVGPaint();

		// If fill colour is set to none
		if (fillColorNone == true)
		{
			color.setValueFromString("none");
		}
		else
		{
			color.setRGBColor(btnFillColor.getBackground());
		}

		return color;

	}

	/**
	 * Set the fill colour
	 * 
	 * @param c
	 *            The colour
	 */
	public void setFillColor(Color c)
	{

		// If colour was none
		if (c == null)
		{
			fillColorNone = true;
		}
		else
		{
			fillColorNone = false;
			btnFillColor.setBackground(c);
		}
	}

	/**
	 * Sets the stroke colour
	 * 
	 * @param c
	 *            The colour
	 */
	public void setStrokeColor(Color c)
	{
		// If colour is null
		if (c == null)
			strokeColorNone = true;

		else
		{
			strokeColorNone = false;
			btnStrokeColor.setBackground(c);
		}
	}

}
